//
//  SDHSportsMatchLiveVC.m
//  SDHSportsNews
//
//  Created by MichaelShan on 2021/3/12.
//

#import "SDHSportsMatchLiveVC.h"
#import "SDHSportsNotchScreenUtil.h"
#import "SDHSportsMatchLiveCell.h"
#import "SDHSportsNetRequest.h"
#import "SDHSportsRequest.h"
#import "MJExtension.h"
#import "SDHSportsMatchLiveModel.h"
#import "UIImageView+YQAdd.h"
#import "SDHSportsLiveHeaderView.h"
#import "MJRefresh.h"
#import "SDHSportsDataManager.h"

@interface SDHSportsMatchLiveVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) SDHSportsLiveHeaderView *headerView;

@property (nonatomic, strong) UILabel *liveTextLb;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation SDHSportsMatchLiveVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"Live";
    self.view.backgroundColor = UIColorFromRGB(48, 48, 48);
    _dataArray = [NSMutableArray array];
    
    [self initUI];
    [self initTable];
    [self loadData];
    [self updateHeaderView];
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"Follow" style:UIBarButtonItemStylePlain target:self action:@selector(rightClick:)];
    self.navigationItem.rightBarButtonItem = right;
}

- (void)rightClick:(id)sender {
    [MBProgressHUD showSuccess:@"Follow succeed!"];
    [[SDHSportsDataManager sharedManager] addFollowMatch:_model];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    
    [[SDHSportsDataManager sharedManager] addBrowseMatch:_model];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
}

- (void)loadData {
    SDHSportsRequest *req = [[SDHSportsRequest alloc] init];
    req.requestUrl = @"http://jsuywu8.com/guojitiyu/Tiyusaishi/getMatch";
    req.requestPara = @{@"match_id":@5730657};
    
    __weak typeof(self) weakSelf = self;
    [MBProgressHUD showLoading];
    [[SDHSportsNetRequest sharedRequest] sendRequest:req returnValueBlock:^(id  _Nonnull obj) {
        [weakSelf parseData:obj];
        
        [weakSelf endLoading];
    } errorBlock:^(id  _Nonnull obj) {
        [weakSelf endLoading];
    } failureBlock:^{
        [weakSelf endLoading];
    }];
}

- (void)parseData:(NSDictionary *)obj {
    [_dataArray removeAllObjects];
    _dataArray = [SDHSportsMatchLiveModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
}

- (void)endLoading {
    [MBProgressHUD hideHUD];
    [self.tableView.mj_header endRefreshing];
    [self.tableView reloadData];
}

- (void)initTable {
    UITableView *table = [UITableView new];
    table.delegate = self;
    table.dataSource = self;
    table.separatorStyle = UITableViewCellSelectionStyleNone;
    table.rowHeight = UITableViewAutomaticDimension;
    table.estimatedRowHeight = 80;
    table.backgroundColor = UIColorFromRGB(48, 48, 48);
    [self.view addSubview:table];
    self.tableView = table;
    
    [table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.equalTo(self.liveTextLb.mas_bottom).offset(0);
        make.bottom.mas_equalTo(0);
    }];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
}

- (void)initUI {
    UIView *headerView = [SDHSportsLiveHeaderView new];
    [self.view addSubview:headerView];
    self.headerView = headerView;
    CGFloat statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
    CGFloat navHeight = self.navigationController.navigationBar.frame.size.height;
    CGFloat notchHeight = [SDHSportsNotchScreenUtil getIPhoneNotchScreenHeight];
    notchHeight = 0;
    CGFloat posY =  statusBarHeight + navHeight + notchHeight + 10;
    
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(posY);
        make.height.mas_equalTo(kScoreHeaderHeight);
    }];
    
    UILabel *liveTextLb = [UILabel new];
    liveTextLb.text = @"Live Text";
    liveTextLb.textColor = [UIColor whiteColor];
    liveTextLb.font = [UIFont boldSystemFontOfSize:18];
    liveTextLb.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:liveTextLb];
    self.liveTextLb = liveTextLb;
    
    [liveTextLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.equalTo(headerView.mas_bottom).offset(15);
        make.height.mas_equalTo(30);
    }];
}

- (void)updateHeaderView {
    [self.headerView updateContent:_model];
}

#pragma mark - tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SDHSportsMatchLiveCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[SDHSportsMatchLiveCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    SDHSportsMatchLiveModel *model = [_dataArray objectAtIndex:indexPath.row];
    [cell updateContent:model];
    
    return cell;
}

@end
